import { Entity, Column, Index, ManyToOne, ManyToMany, JoinTable, JoinColumn } from 'typeorm';
import { BaseEntity } from '@app/common/entities/base.entity';

// 临时的 Author 和 Category 实体接口，后续可以创建独立的实体
export interface Author {
  id: number;
  username: string;
  nickname?: string | null;
  email?: string;
  avatar?: string | null;
  bio?: string | null;
  role?: string;
  status?: string;
  lastLoginAt?: string;
  createdAt?: string;
  updatedAt?: string;
}

export interface Category {
  id: number;
  name: string;
}

export interface Tag {
  id: number;
  name: string;
}

@Entity('posts')
@Index(['slug'])
export class Post extends BaseEntity {
  @Column({ type: 'varchar', length: 255 })
  title: string;

  @Column({ type: 'varchar', length: 255 })
  slug: string;

  @Column({ type: 'text', nullable: true })
  excerpt?: string;

  @Column({ type: 'longtext' })
  content: string;

  @Column({
    type: 'enum',
    enum: ['draft', 'published', 'archived'],
    default: 'draft'
  })
  status: 'draft' | 'published' | 'archived';

  @Column({ type: 'int', default: 0 })
  viewCount: number;

  @Column({ type: 'int', default: 0 })
  likeCount: number;

  @Column({ type: 'int', default: 0 })
  commentCount: number;

  @Column({ type: 'boolean', default: false })
  isTop: boolean;

  @Column({ type: 'boolean', default: true })
  allowComment: boolean;

  // SEO 相关字段
  @Column({ type: 'varchar', length: 255, nullable: true })
  metaTitle?: string | null;

  @Column({ type: 'text', nullable: true })
  metaDescription?: string | null;

  @Column({ type: 'text', nullable: true })
  metaKeywords?: string | null;

  @Column({ type: 'varchar', length: 500, nullable: true })
  canonicalUrl?: string | null;

  @Column({ type: 'varchar', length: 255, nullable: true })
  robotsMeta?: string | null;

  // Open Graph 相关字段
  @Column({ type: 'varchar', length: 255, nullable: true })
  ogTitle?: string | null;

  @Column({ type: 'text', nullable: true })
  ogDescription?: string | null;

  @Column({ type: 'varchar', length: 500, nullable: true })
  ogImage?: string | null;

  // 百度结构化数据
  @Column({ type: 'text', nullable: true })
  baiduSchema?: string | null;

  // 微信分享相关
  @Column({ type: 'varchar', length: 255, nullable: true })
  wxShareTitle?: string | null;

  @Column({ type: 'text', nullable: true })
  wxShareDescription?: string | null;

  @Column({ type: 'varchar', length: 500, nullable: true })
  wxShareImage?: string | null;

  // 通用分享相关
  @Column({ type: 'varchar', length: 255, nullable: true })
  shareTitle?: string | null;

  @Column({ type: 'text', nullable: true })
  shareDescription?: string | null;

  @Column({ type: 'varchar', length: 500, nullable: true })
  shareImage?: string | null;

  // 其他字段
  @Column({ type: 'int', nullable: true })
  readingTime?: number | null;

  @Column({ type: 'varchar', length: 10, nullable: true })
  language?: string | null;

  @Column({ type: 'text', nullable: true })
  altText?: string | null;

  @Column({ type: 'json', nullable: true })
  slugHistory?: string[];

  @Column({ type: 'timestamp', nullable: true })
  publishedAt?: Date | null;

  // 作者关联 (暂时用 JSON 存储，后续可以改为真正的关联)
  @Column({ type: 'json' })
  author: Author;

  @Column({ type: 'int' })
  authorId: number;

  // 分类关联 (暂时用 JSON 存储，后续可以改为真正的关联)
  @Column({ type: 'json', nullable: true })
  category?: Category | null;

  @Column({ type: 'int', nullable: true })
  categoryId?: number | null;

  // 标签关联 (暂时用 JSON 存储，后续可以改为真正的关联)
  @Column({ type: 'json', nullable: true })
  tags: Tag[];

  @Column({ type: 'varchar', length: 500, nullable: true })
  featuredImage?: string | null;
}